//
//  ViewController.m
//  LyyTextView
//
//  Created by ylu on 15-3-25.
//  Copyright (c) 2015年 lyy. All rights reserved.
//

#import "ViewController.h"
#import "LyyTextView.h"

@interface ViewController ()

@property (strong, nonatomic)LyyTextView *textView;
@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    监听键盘弹出
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handelKeyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    
//    监听键盘消失
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handelKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textView];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (LyyTextView *)textView{
    if (!_textView) {
        _textView = [[LyyTextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) withTopLabelOffset:6.0f];
        _textView.placeholderText = @"some text here...";
        _textView.font = [UIFont systemFontOfSize:16.0f];
    }
    return _textView;
}
- (IBAction)hiddKeyboard:(UIBarButtonItem *)sender {
    
    [self.view endEditing:YES];
}

- (void)handelKeyboardDidShow:(NSNotification *)notification{
    
    NSValue *keyboardRectAsObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect;
    [keyboardRectAsObject getValue:&keyboardRect];
//    self.textView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, keyboardRect.size.height, 0.0f);
    CGRect textViewRect = self.textView.frame;
    textViewRect.size.height = self.view.frame.size.height - keyboardRect.size.height-10;
    self.textView.frame = textViewRect;
}
- (void)handelKeyboardWillHide:(NSNotification *)notification{
    
    CGRect textViewRect = self.textView.frame;
    textViewRect.size.height = self.view.frame.size.height;
    self.textView.frame = textViewRect;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
